package com.springboot.cloud.sysadmin.organization.rest;

import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.utils.StringUtils;
import com.springboot.cloud.sysadmin.organization.entity.form.*;
import com.springboot.cloud.sysadmin.organization.entity.param.RoleQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Position;
import com.springboot.cloud.sysadmin.organization.entity.po.Role;
import com.springboot.cloud.sysadmin.organization.service.IRoleService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色")
@Slf4j

public class RoleController {
    @Autowired
    private   IRoleService roleService;

    @ApiOperation(value = "新增角色", notes = "新增一个角色")
    @ApiImplicitParam(name = "roleForm", value = "新增角色form表单", required = true, dataType = "RoleForm")
    @PostMapping
    public Result add(@Valid @RequestBody RoleForm roleForm) {
        log.debug("roleForm:{}", roleForm);
        Role role = roleForm.toPo(Role.class);
        if (roleService.add(role)) {
            return Result.success("新增角色成功");
        } else {
            return Result.error("已存在相同名称的角色");
        }
    }

    /**
     * 状态修改
     */
    @ApiOperation(value = "修改角色状态", notes = "角色id和状态修改角色状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "roleUpdateEnabled", value = "角色实体", required = true, dataType = "RoleUpdateEnabled")})
    @ApiResponses(@ApiResponse(code = 200, message = "处理成功", response = Result.class))
    @PutMapping("/changeEnabled")
    public Result changeEnabled(@Valid @RequestBody RoleUpdateEnabledForm roleUpdateEnabledForm) {
        Role role = roleUpdateEnabledForm.toPo(Role.class);
        if (roleService.updateEnabled(role)) {
            return Result.success("更新成功!!");
        }
        return Result.error("更新角色状态失败，请检查有无该角色");
    }

    @ApiOperation(value = "删除角色", notes = "根据url的id来指定删除对象")
    @DeleteMapping
    public Result delete(@Valid @RequestBody Set<String> ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error("没选中要删除的资源");
        } else if (roleService.delete(ids)) {
            return Result.success("删除角色成功");
        }
        log.debug("idsa:{}", ids);
        return Result.error("没选中要删除的角色");

    }

    @ApiOperation(value = "修改角色菜单", notes = "修改角色菜单")
    @PutMapping(value = "/menu/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody RoleResourceForm roleResourceForm) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        if (roleService.updateResource(roleResourceForm)) {
            return Result.success("修改角色权限成功");
        }
        return Result.error("更新失败");

    }

    @ApiOperation(value = "修改角色", notes = "修改指定角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "roleForm", value = "角色实体", required = true, dataType = "roleForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody RoleForm roleForm) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        Role role = roleForm.toPo(id, Role.class);
        if (roleService.update(role)) {
            return Result.success("更新成功!");
        }
        return Result.error("更新失败");

    }

    @ApiOperation(value = "获取角色", notes = "获取指定角色信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "角色ID", required = true, dataType = "string")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        log.info("get with id:{}", id);
        if (roleService.get(id)) {
            return Result.success(roleService.get(id));
        }
        return Result.error("查找失败");

    }

    @ApiOperation(value = "获取所有角色", notes = "获取所有角色")
    @GetMapping(value = "/all")
    public Result get() {
        return Result.success(roleService.getAll());
    }

    @ApiOperation(value = "查询角色", notes = "根据用户id查询用户所拥有的角色信息")
    @ApiImplicitParam(paramType = "path", name = "userId", value = "用户id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping(value = "/user/{userId}")
    public Result query(@PathVariable String userId) {
        if (StrUtil.isBlank(userId)) {
            return Result.error("没有用户的id，无法查询");
        }
        if (CollectionUtils.isEmpty(roleService.query(userId))) {
            return Result.error("查找不到对应值");
        }
        return Result.success(roleService.query(userId));

    }

    @ApiOperation(value = "搜索角色", notes = "根据条件搜索角色信息")
    @ApiImplicitParam(name = "roleQueryForm", value = "角色查询参数", required = true, dataType = "RoleQueryForm")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @PostMapping(value = "/conditions")
    public Result query(@Valid @RequestBody RoleQueryForm roleQueryForm) {
        if (roleQueryForm.getCreatedTime() != null && !roleQueryForm.getCreatedTime().isEmpty()) {
            roleQueryForm.setCreatedTimeStart(roleQueryForm.getCreatedTime().get(0));
            roleQueryForm.setCreatedTimeEnd(roleQueryForm.getCreatedTime().get(1));
        }
        log.debug("query with name:{}", roleQueryForm);
        return Result.success(roleService.query(roleQueryForm.getPage(), roleQueryForm.toParam(RoleQueryParam.class)));
    }
}
